<template>
	<view class="">
		<u-navbar title="个人信息" autoBack placeholder></u-navbar>
		<view class="p-2 bg-white flex align-center border-bottom" @click="changeUserLogo">
			<view class="font-28 text-light3F">头像</view>
			<view class="ml-auto flex align-center">
				<u-avatar :src="avatar?avatar:'/static/user/Mask group.png'" size="34"></u-avatar>
				<view class="ml-1">
					<u-icon name="arrow-right" ></u-icon>
				</view>
			</view>
		</view>
		<view class="p-2 bg-white flex align-center border-bottom" @click="show=true">
			<view class="font-28 text-light3F">昵称</view>
			<view class="ml-auto flex align-center">
				<view class="font-28 text-main">{{nickname}}</view>
				<view class="ml-1">
					<u-icon name="arrow-right" ></u-icon>
				</view>
			</view>
		</view>
		
		
		
		<template>
			<u-popup :show="show" @close="show=false" closeOnClickOverlay mode='center' round="16rpx">
				<view class="poupContent">
					<view class="poupContent_title">设置昵称</view>
					<view class="flex align-center justify-center px-2">
						<u-input v-model="nickname" placeholder="请输入昵称" border="none" clearable></u-input>
					</view>
					<view class="" style="flex: 1;display: flex;flex-direction: column;justify-content: flex-end;">
						<view class="poupContent_operation">
							<view class="poupContent_operation_cancel" @click="show=false">取消</view>
							<view class="poupContent_operation_sure" @click="changeUserName">确定</view>
						</view>
					</view>
				</view>
			</u-popup>
		</template>
		
	</view>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from '@/utils/image-tools.js'
	
	import {
		base
	} from '@/request/env.js'
	
	export default{
		data(){
			return{
				dataInfo:{},
				avatar:'',
				nickname:'',
				show:false,
				URL:base
			}
		},
		onLoad() {
			this.getPersonal()
		},
		methods:{
			getPersonal() {
				this.$api.user().then(res => {
					if (res.code === 1) {
						this.avatar = res.data.avatar
						console.log(this.avatar)
						this.nickname = res.data.nickname
					}
				})
			},
			// 主图
			changeUserLogo() {
				uni.chooseImage({
					count: 1, // 上传个数
					sourceType: ['album','camera'], //从相册选择
					success: (res) => {
						let igmFile = res.tempFilePaths;
						let that = this
						uni.uploadFile({
							url: base+'/api/index/uploadData',
							method: "POST",
							filePath: igmFile[0],
							name: 'file',
							formData: {
								filename: 'file',
								token:uni.getStorageSync('token')
							},
							success: (res) => {
								let data = JSON.parse(res.data)
								that.storeImage(data.data.url)		
							},
							fail(error) {
								console.log(error)
							}
						})
					}
				})
			},
			storeImage(url){
				this.$api.myprofile({
					avatar:url
				}).then(res=>{
					if(res.code === 1){
						uni.showToast({
							title: '设置成功',
							icon:'none'
						});
						this.getPersonal()
					}
				})
			},
			changeUserName(){
				this.$api.myprofile({
					nickname:this.nickname
				}).then(res=>{
					if(res.code === 1){
						uni.showToast({
							title: '设置成功',
							icon:'none'
						});
						this.show = false
						this.getPersonal()
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.poupContent {
		width: 640rpx;
		height: 350rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		display: flex;
		flex-direction: column;

		&_title {
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 34rpx;
			font-weight: bold;
			color: rgba(0, 0, 0, 0.9);
			padding: 64rpx 0 0 0;
		}

		&_desc {
			display: flex;
			align-items: center;
			justify-content: center;
			text-align: center;
			padding: 0 48rpx;
			font-size: 34rpx;
			font-weight: 500;
			color: #353535;
			margin-top: 68rpx;
		}

		&_operation {
			display: flex;
			align-items: flex-end;
			flex: 1;

			&_cancel {
				height: 112rpx;
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: center;
				border-top: 1rpx solid rgba(0, 0, 0, 0.10);
				border-right: 1rpx solid rgba(0, 0, 0, 0.10);
				font-size: 34rpx;
				font-weight: 500;
				color: #000000;
			}

			&_sure {
				border-top: 1rpx solid rgba(0, 0, 0, 0.10);
				height: 112rpx;
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 34rpx;
				font-weight: 500;
				color: #04BF79;
			}
		}
	}

	
</style>